<script setup lang="ts">
import { ref } from "vue";
import { userPermission } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import EditPen from "@iconify-icons/ep/edit-pen";
import Delete from "@iconify-icons/ep/delete";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "Permission"
});

const formRef = ref();
const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  groupOptions,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  openMenuDialog,
  handleSizeChange,
  handleCurrentChange
} = userPermission();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="权限名称：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入权限名称"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>
      <el-form-item label="权限代码：" prop="code">
        <el-input
          v-model="form.code"
          placeholder="请输入权限代码"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="权限分组：" prop="group">
        <el-select
          v-model="form.group"
          filterable
          clearable
          placeholder="请选择权限分组"
          class="!w-[200px]"
        >
          <el-option
            v-for="item in groupOptions"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" prop="state">
        <DictOption
          v-model="form.state"
          code="State"
          type="select"
          placeholder="请选择状态"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar title="权限列表" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <Auth value="add_permission">
          <el-button
            type="primary"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog()"
          >
            新增权限
          </el-button>
        </Auth>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :size="size"
          adaptive
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small'"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <Auth value="edit_permission">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
                @click="openDialog('编辑', row)"
              >
                编辑
              </el-button>
            </Auth>
            <Auth value="delete_permission">
              <el-popconfirm
                :title="`是否确认删除权限名称为${row.name}的这条数据`"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(Delete)"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </Auth>
            <Auth value="permission_bind_menu">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="openMenuDialog(row.id, row.name)"
              >
                分配菜单
              </el-button>
            </Auth>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
